//
//  GGTDataBaseHandler.m
//  GGT
//
//  Created by 徐继垚 on 14/11/11.
//  Copyright (c) 2014年 GGT. All rights reserved.
//

#import "GGTDataBaseHandler.h"

@implementation GGTDataBaseHandler
//单例的产生方法:(加号方法)
+ (GGTDataBaseHandler *)shareInstance
{
    //当程序第一次调用这个方法,就创建一个静态的指针
    static GGTDataBaseHandler *handler = nil;
    //当指针指向空时,创建一个新的对象
    if (handler == nil) {
        handler = [[GGTDataBaseHandler alloc]init];
        [handler openDB];
    }
    //把对象返回
    return handler;
}
//打开数据库
- (void)openDB
{
    //打开数据库
    
    
    
    //如果路径下有文件,就直接用数据库指针指向他,如果没有 创建一个
    //参数1:文件名 数据库文件的路径
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *dataBasePath = [docPath stringByAppendingPathComponent:@"database.db"];
    NSLog(@"%@",dataBasePath);
    //参数2: 数据库指针的地址
    
    int result =  sqlite3_open([dataBasePath UTF8String], &dbPoint);
    NSLog(@"打开数据库结果 ; %d",result);
    if (SQLITE_OK == result) {
        NSLog(@"打开数据库成功");
    }

}
//关闭数据库
- (void)closeDB
{
    //一般来说,可以一直让数据库保持打开状态,关闭的方法可以不用
    sqlite3_close(dbPoint);
}
//创建表
- (void)creatTable
{
    
    //执行sql语句,对本地数据库做相应的处理
    //参数1:数据库指针
    //参数2:要执行的sql语句
    //参数3:回调的函数指针
    NSString *sql = [NSString stringWithFormat:@"create table teamBuyData(title text, descript text, current_price text, list_price text, image text, number text,deal_id text primary key)"];
    
    int result = sqlite3_exec(dbPoint, [sql UTF8String], NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"创建成功");
    } else {
        NSLog(@"错误编号: %d",result);
    }
}
//添加一条数据
- (void)insertTeamBuyData:(GGTTeamBuyDataModel *)teamBuyData
{
    NSString *sql = [NSString stringWithFormat:@"insert into teamBuyData values('%@','%@','%@','%@','%@','%@','%@')",teamBuyData.title, teamBuyData.descript, teamBuyData.current_price, teamBuyData.list_price, teamBuyData.image,teamBuyData.number,teamBuyData.deal_id];
    int result = sqlite3_exec(dbPoint, [sql UTF8String], NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    } else {
        NSLog(@"错误编号: %d",result);
    }
}

//删除一条数据
- (void)deleteTeamBuyDataLieming:(NSString *)lieming
                       chaxunzhi:(NSString *)chaxunzhi
{
    NSString *sql = [NSString stringWithFormat:@"delete from teamBuyData where %@ = %@",lieming,chaxunzhi];
    int result = sqlite3_exec(dbPoint, [sql UTF8String], NULL, NULL, NULL);
    
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"错误编号: %d",result);
    }
}
//查询所有数据
- (NSMutableArray *)selectAll
{
    //创建一个可变数组,用来存储查询到的所有数据对象(student)
    NSMutableArray *arr = [NSMutableArray array];
    //1.创建一个数据库的替身,临时存储所有的对数据库的操作,防止直接访问数据库参数的数据风险
    sqlite3_stmt *stmt = nil;
    //2.检查sql语句的写法
    NSString *sql = [NSString stringWithFormat:@"select * from teamBuyData"];
    //参数1:数据库指针
    //参数2:sql语句
    //参数3:限制sql语句的长度(-1的时候表示不限制)
    //参数4:替身
    //作用:检查sql语句的正确性,将stmt指针指向数据库文件,作为替身,临时存储所有对数据库的操作.
    int result = sqlite3_prepare_v2(dbPoint, [sql UTF8String], -1, &stmt, NULL);
    if (result == SQLITE_OK) {
        //如果sql语句检查无误,开始执行查询,检索数据库的数据
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            //从替身中取得每一行数据
            //参数1:替身
            //参数2:第几列

            const  char *titleChar = (const char *)sqlite3_column_text(stmt, 0);
            NSString *title = [NSString stringWithUTF8String:titleChar];
            
            const char *descriptChar = (const char *)sqlite3_column_text(stmt, 1);
            NSString *descript = [NSString stringWithUTF8String:descriptChar];
     //注意
           const  int current_priceInt = (const int )sqlite3_column_int(stmt, 2);
            NSNumber *current_price = [NSNumber numberWithInt:current_priceInt];
            const  int list_priceInt = (const int )sqlite3_column_int(stmt, 2);
            NSNumber *list_price = [NSNumber numberWithInt:list_priceInt];

          
            const char *imageChar = (const char *)sqlite3_column_text(stmt, 4);
            NSString *image = [NSString stringWithUTF8String:imageChar];
        
            const  int numberInt = (const int )sqlite3_column_int(stmt, 5);
            NSNumber *number = [NSNumber numberWithInt:numberInt];
            
            const char *deal_idChar = (const char *)sqlite3_column_text(stmt, 6);
            NSString *deal_id = [NSString stringWithUTF8String:deal_idChar];
            //取得的值传给student对象
            GGTTeamBuyDataModel *teamBuyData = [[GGTTeamBuyDataModel alloc]init];
            teamBuyData.title = title;
            teamBuyData.descript = descript;
            teamBuyData.current_price= current_price;
            teamBuyData.list_price = list_price;
            teamBuyData.image = image;
            teamBuyData.deal_id = deal_id;
            teamBuyData.number = number;
#pragma 上面的 都没改 小心
            //将创建好的学生对象添加到数组中
            [arr addObject:teamBuyData];
            [teamBuyData release];
        }
        NSLog(@"查询成功");
    }
    //将替身中的所有操作,写入本地数据库,销毁替身
    sqlite3_finalize(stmt);
    return arr;
}

@end
